/**
 * PictureUploadDAO.java
 * Creates a data access object (DAO) for an image data transfer object (DTO).
 */
package com.example.myproject;

import java.util.List;
import com.googlecode.objectify.Key;
import com.googlecode.objectify.ObjectifyService;
import com.googlecode.objectify.util.DAOBase;

public class PictureUploadDAO extends DAOBase{

    static {
        ObjectifyService.register(PictureUploadDTO.class);
    }

    /**
     * Saves an PictureUploadDTO to the blobstore.
     * @param dto The image object to be saved.
     */  
    public PictureUploadDTO save(PictureUploadDTO dto) {
    	ofy().put(dto);
        return dto;
    }
 
    /**
     * Retrieves PictureUploadDTOs from the blobstore with matching IDs.
     * @param id Supplied search parameter.
     * @return Returns a key map of PictureUploadDTOs matching id.
     */	
    public PictureUploadDTO findById(Long id) {
    	if(id <= 1L) return null;
        Key<PictureUploadDTO> key = new Key<PictureUploadDTO>(PictureUploadDTO.class, id);
        return ofy().get(key);
    }
    
    /**
     * Retrieves a list of all PictureUploadDTOs in the blobstore.
     * @return List containing all PictureUploadDTOs in the blobstore.
     */    	
	public List<PictureUploadDTO> list() 
	{		
		List<PictureUploadDTO> alist = ofy().query(PictureUploadDTO.class).list();
		return alist;
	}

    /**
     * Retrieves a list of all PictureUploadDTOs in the blobstore whose caption
     * matches the supplied string.
     * @param caption Supplied search parameter.
     * @return List containing PictureUploadDTOs with matching captions.
     */		
	public List<PictureUploadDTO> listByCaption(String caption) 
	{	
		System.out.println("caption======"+caption);
		List<PictureUploadDTO> alist = ofy().query(PictureUploadDTO.class).filter("caption", caption).list();
		return alist;
	}
    
    /**
     * Deletes a PictureUploadDTO from the blobstore, specified by id.
     * @param id Supplied id of the PictureUploadDTO to be deleted.
     */
	public void delete(Long id) 
	{	
		if(id <= 1L) return ;
		PictureUploadDTO picture = ofy().query(PictureUploadDTO.class).filter("id ", id).get();
		ofy().delete(picture);
	}
	
    /**
     * Deletes a PictureUploadDTO from the blobstore, specified by parentId.
     * @param parentId Supplied parentId of the PictureUploadDTO to be deleted.
     */
	public void deleteByParentId(Long parentId) 
	{		
		if(parentId <= 1L) return ;
		PictureUploadDTO picture = ofy().query(PictureUploadDTO.class).filter("parentId ", parentId).get();
		ofy().delete(picture);
	}
	
}
